<?php
	class common extends Mysql{
		function __construct(){
			parent::__construct();
		}
		
		/**
		 * 获得数量
		 * @param $table
		 * @param $where
		 * @param $left
		 * @param $group
		 */
		function getNum($sql,$key="",$group=false){
			//是否有group by
			if(strpos($sql, "group by")){
				$group = true;
			}
			
			if($key!='' && $key!="*"){
				$key = "count(*) as num,".$key;
			}else{
				$key = "count(*) as num";
			}
			$sql="select ".$key." from ".$sql;
			if($group==true) $sql = "select count(*) as num from (".$sql.") as test";
			$query=$this->query($sql);
			$row=$this->fetch_array($query);
			$num=$row['num'];
			return $num;
		}
		
		/**
		 * 获得列表
		 * @param $table
		 * @param $key
		 * @param $where
		 * @param $left
		 * @param $page
		 */
		function getList($sql, $key="*",$num=0,$debug='0'){
			$sqlNum=$sql;
			$sql="select ".$key." from ".$sql;
			if($debug=='1') echo $sql."<br/>";
			if($num!=0){
				$totals=$this->getNum($sqlNum,$key);
				$pagec = new Pages($totals,$num);
				$query=$this->query($sql." limit ".$pagec->limit['a'].",".$pagec->limit['b']);
				$return['page']=$pagec->firstpage().$pagec->prepage().$pagec->more_page().$pagec->nextpage().$pagec->lastpage();
			}else{
				$query=$this->query($sql);
			}
			while($row=$this->fetch_array($query)){
				$result[]=$row;
			}
			$return['result']=$result;
			
			return $return;
		}
		
		/**
		 * 获得内容
		 * @param $table
		 * @param $where
		 * @param $key
		 */
		function getInfo($sql, $key='*',$debug='0'){
			$sql="select ".$key." from ".$sql;
			if($debug=='1') echo $sql."<br/>";
			$query=$this->query($sql);
			$result=$this->fetch_array($query);
			return $result;
		}
		
		/**
		 * 上传文件
		 * @param $name
		 * @param $dir
		 */
		function uploadFile($name,$dir){
			$File = upload::uploadfile($ARG=array(
					'File'     => array('name'=>$_FILES[$name]['name'],'tmp_name' => $_FILES[$name]['tmp_name']),
					'Dir'=>$dir
			));
		
			$file=$File['newname'];
		
			return $file;
		}
		
		/**
		 * 加密算法
		 * @param $data
		 * @param $key
		 */
		function encrypt($data, $key='runer'){
			$key = md5($key);
			$x = 0;
			$len = strlen($data);
			$l = strlen($key);
			for($i = 0; $i < $len; $i++){
				if ($x == $l){
					$x = 0;
				}
				$char .= $key{$x};
				$x++;
			}
			for($i = 0; $i < $len; $i++){
				$str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
			}
			return urlencode(base64_encode($str));
		}
		
		/**
		 * 解密算法
		 * @param $data
		 * @param $key
		 */
		function decrypt($data, $key='runer'){
			$key = md5($key);
			$x = 0;
			$data = base64_decode(urldecode($data));
			$len = strlen($data);
			$l = strlen($key);
			for($i = 0; $i < $len; $i++){
				if ($x == $l){
					$x = 0;
				}
				$char .= substr($key, $x, 1);
				$x++;
			}
			for($i = 0; $i < $len; $i++){
				if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))){
					$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
				}else{
					$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
				}
			}
			return $str;
		}
		
		/**
		 * 获得网站设置
		 */
		function _setting(){
			$sql = "setting";
			$setting = $this->getList($sql);
			$_setting = array();
			if(is_array($setting['result'])){
				foreach($setting['result'] as $k=>$v){
					$_setting[$v['key']] = $v['value'];
				}
			}
			return $_setting;
		}

		/**
		 * 二维数组转换成字符串3.0版本
		 * @author --wxy
		 * @param 	array 	$arr
		 * @param	string	$key
		 * @return 	string
		 */
		function convert2($arr,$key='id'){
			$t="";
			if(!empty($arr)){
				foreach($arr as $v){
					$temp[]= $v[$key];
				}
				$temp = array_unique($temp);
				$t = join(",",$temp);
			}
			$t=$t?$t:0;
			return $t;
		}

		function submitAlert($msg,$url){
			return "<script>
					alert('".$msg."');
					window.location.href='".$url."';
				   </script>";
		}

		function submitAlertLayer($msg,$url){
			return "<script>
					layer.alert('".$msg."');
					window.location.href='".$url."';
				   </script>";
		}
		
	}
?>